package com.dfiles.provider;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.dfiles.bean.DFile;
import com.dfiles.provider.FileTypeHelper.FileTypeAll;
import com.dfiles.utils.Pinyin;

public class FileOrderHelper {
	public final static List<Object> spinnerList = new ArrayList<Object>();
	static {
		spinnerList.add("文件名称");
		spinnerList.add("类型");
		spinnerList.add("大小");
		spinnerList.add("修改日期");
	}

	// =================================================
	/**
	 * 根据修改日期排序
	 * 
	 * @param list
	 */
	public static void sortByModifyDate(List<DFile> list, final boolean modifyDatePositive) {
		if (list == null) {
			return;
		}
		Collections.sort(list, new Comparator<DFile>() {

			@Override
			public int compare(DFile lhs, DFile rhs) {
				Long l = null;
				Long r = null;
				if (modifyDatePositive) {
					l = lhs.lastModify;
					r = rhs.lastModify;
				} else {
					l = rhs.lastModify;
					r = lhs.lastModify;
				}
				return l.compareTo(r);
			}
		});

	}

	/**
	 * 根据名称排序
	 * 
	 * @param list
	 */
	public static void sortByName(List<DFile> list, final boolean namePositive) {
		if (list == null) {
			return;
		}
		Collections.sort(list, new Comparator<DFile>() {

			@Override
			public int compare(DFile lhs, DFile rhs) {
				String l = null;
				String r = null;
				if (namePositive) {
					l = lhs.name;
					r = rhs.name;
				} else {
					r = lhs.name;
					l = rhs.name;
				}

				l = Pinyin.converterToFirstSpell(l);
				r = Pinyin.converterToFirstSpell(r);

				return l.compareToIgnoreCase(r);
			}
		});
	}

	/**
	 * 根据类型排序
	 * 
	 * @param list
	 */
	public static void sortBySize(List<DFile> list, final boolean sizePositive) {
		if (list == null) {
			return;
		}
		Collections.sort(list, new Comparator<DFile>() {

			@Override
			public int compare(DFile lhs, DFile rhs) {
				Long l = null;
				Long r = null;
				if (sizePositive) {
					l = lhs.size;
					r = rhs.size;
				} else {
					l = rhs.size;
					r = lhs.size;
				}
				return l.compareTo(r);
			}
		});
	}

	/**
	 * 根据类型排序
	 * 
	 * @param list
	 */
	public static void sortByType(List<DFile> list, final boolean typePositive) {
		if (list == null) {
			return;
		}
		Collections.sort(list, new Comparator<DFile>() {
			@Override
			public int compare(DFile lhs, DFile rhs) {
				FileTypeAll l = null;
				FileTypeAll r = null;
				if (typePositive) {
					l = lhs.type;
					r = rhs.type;
				} else {
					r = lhs.type;
					l = rhs.type;
				}
				return l.compareTo(r);
			}
		});
	}

}
